package com.cdtu.store.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.store.common.utils.PageUtils;
import com.cdtu.store.common.utils.Query;
import com.cdtu.store.item.dao.SpuDao;
import com.cdtu.store.common.entity.SpuEntity;
import com.cdtu.store.item.service.BrandService;
import com.cdtu.store.item.service.CategoryService;
import com.cdtu.store.item.service.SpuService;
import com.cdtu.store.item.service.SpuDetailService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



@Service("spuService")
public class SpuServiceImpl extends ServiceImpl<SpuDao, SpuEntity> implements SpuService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private SpuDetailService spuDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SpuEntity> wrapper = new QueryWrapper<SpuEntity>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.and((w) -> {
                w.eq("id",key).or().like("title",key);
            });
        }
        String saleable = (String) params.get("saleable");
        if (!StringUtils.isEmpty(saleable)){
            wrapper.eq("saleable",saleable);
        }
        String valid = (String) params.get("valid");
        if (!StringUtils.isEmpty(valid)){
            wrapper.eq("valid",valid);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)){
            wrapper.eq("brand_id",brandId);
        }
        String catelogPath = (String) params.get("catelogPath");
        if (!StringUtils.isEmpty(catelogPath)){
            String[] cat = catelogPath.split(",");
            wrapper.eq("cid1",cat[0]);
            wrapper.eq("cid2",cat[1]);
            wrapper.eq("cid3",cat[2]);
        }
        IPage<SpuEntity> page = this.page(
                new Query<SpuEntity>().getPage(params),
                wrapper
        );
        List<SpuEntity> list = page.getRecords();
        list.forEach((stock -> {
            stock.setBname(brandService.getById(stock.getBrandId()).getName());
            stock.setSpuDetail(spuDetailService.getById(stock.getId()));
        }));
        return new PageUtils(page);
    }

    @Override
    public Page<SpuEntity> querySpu(Map<String, Object> params, Integer saleable) {
        String key = (String) params.get("key");
        QueryWrapper<SpuEntity> wrapper = new QueryWrapper<>();
        if(saleable != null){
            wrapper.eq("saleable",saleable);
        }
        if(!StringUtils.isBlank(key)){
            wrapper.and(obj ->{
                obj.like("title",key).or().like("sub_title",key);
            });
        }
        IPage<SpuEntity> page = this.page(new Query<SpuEntity>().getPage(params),wrapper);
        List<SpuEntity> spuEntities = page.getRecords();
        spuEntities.forEach(spu ->{
            List<String> names = this.categoryService.queryNamesByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));
            spu.setCname(StringUtils.join(names,"/"));
            spu.setBname(this.brandService.getById(spu.getBrandId()).getName());
        });
        Page<SpuEntity> spuEntityPage = new Page<>();
        BeanUtils.copyProperties(page,spuEntityPage);
        return spuEntityPage;
    }

}